﻿#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:6031)

#include "SeqList.h"

//初始化
void SeqListInit(SeqList* ps)
{
	assert(ps);
	//给a分配空间
	ps->a = (SLDateType*)malloc(sizeof(SLDateType) * 4);
	ps->capacity = 4;
	ps->size = 0;
}
//销毁
void SeqListDestroy(SeqList* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->size = 0;
}
//打印
void SeqListPrint(SeqList* ps)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}
//检查空间是否已满
void Seqcheck(SeqList* ps)
{
	assert(ps);
	//满了就增加容量
	if (ps->size == ps->capacity)
	{
		SLDateType* tmp = (SLDateType*)realloc(ps->a, ps->capacity * 2 * sizeof(SLDateType));
		assert(tmp);
		ps->a = tmp;
		ps->capacity *= 2;
	}
}
//尾插
void SeqListPushBack(SeqList* ps, SLDateType x)
{
	Seqcheck(ps);
	assert(ps);
	ps->a[ps->size] = x;
	ps->size++;
}
//头插
void SeqListPushFront(SeqList* ps, SLDateType x)
{
	Seqcheck(ps);
	assert(ps);
	int end = ps->size - 1;
	for (end; end >= 0; end--)
	{
		ps->a[end + 1] = ps->a[end];
	}
	ps->a[0] = x;
	ps->size++;
}
//头删
void SeqListPopFront(SeqList* ps)
{
	assert(ps);
	int front = 0;
	for (front; front < ps->size; front++)
	{
		ps->a[front] = ps->a[front + 1];
	}
	ps->size--;
}
//尾删
void SeqListPopBack(SeqList* ps)
{
	assert(ps);
	ps->size--;
}
// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x)
{
	assert(ps);
	int find = 0;
	for (find; find < ps->size; find++)
	{
		if (ps->a[find] == x)
		{
			return find;
		}
	}
	return NULL;
}
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
	Seqcheck(ps);
	assert(ps);
	int end = ps->size - 1;
	for (end; end >= pos; end--)
	{
		ps->a[end + 1] = ps->a[end];
	}
	ps->a[pos] = x;
	ps->size++;
}
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, int pos)
{
	assert(ps);
	for (pos; pos < ps->size; pos++)
	{
		ps->a[pos] = ps->a[pos + 1];
	}
	ps->size--;
}

